﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace KetBanBonPhuong
{
    /// <summary>
    /// Summary description for FriendList
    /// </summary>
    public class FriendList
    {
        private int friend_list_id;

        public int Friend_list_id
        {
            get { return friend_list_id; }
            set { friend_list_id = value; }
        }

        private int user_id;

        public int User_id
        {
            get { return user_id; }
            set { user_id = value; }
        }

        private string description;

        public string Description
        {
            get { return description; }
            set { description = value; }
        }

        public FriendList()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public FriendList(SqlDataReader sdr)
        {
            if (sdr != null && !sdr.IsClosed)
            {
                friend_list_id = sdr.GetInt32(0);
                if (!sdr.IsDBNull(1))
                    user_id = sdr.GetInt32(1);
                if (!sdr.IsDBNull(2))
                    description = sdr.GetString(2);
            }
        }

        public static DataTable GetAll()
        {
            DBHelp dbh = new DBHelp();
            DataTable dt = new DataTable();
            string cmd = "FriendList_GetAll";
            dt = dbh.ExecuteDataTable(cmd, CommandType.StoredProcedure);
            return dt;
        }


        public static int GetFrienListID(int UserID)
        {
            DBHelp dbh = new DBHelp();
            DataTable dt = new DataTable();
            dbh.AddParameter("@user_id", UserID);
            String cmd = "select * from tbl_friend_list where user_id = @user_id";
            dt = dbh.ExecuteDataTable(cmd, CommandType.Text);
            if (dt.Rows.Count == 0)
            {
                return -1;
            }
            else
            {
                return (int)dt.Rows[0]["friend_list_id"];
            }
        }

        //Ham lay ve ID nhan vao
        public static FriendList GetInfo(int ID)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@friend_list_id", ID);
            FriendList friendList = new FriendList();
            string cmd = "FriendList_GetInfo";
            SqlDataReader sdr = dbh.ExecuteReader(cmd, CommandType.StoredProcedure);
            if (sdr.Read())
            {
                friendList.Friend_list_id = sdr.GetInt32(0);
                if (!sdr.IsDBNull(1))
                    friendList.User_id = sdr.GetInt32(1);
                if (!sdr.IsDBNull(2))
                    friendList.Description = sdr.GetString(2);
            }
            return friendList;
        }

        public static FriendList GetInfoByUserID(int UserID)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@user_id", UserID);
            FriendList friendList = new FriendList();
            string cmd = "select * from tbl_friend_list where user_id = @user_id";
            SqlDataReader sdr = dbh.ExecuteReader(cmd, CommandType.Text);
            if (sdr.Read())
            {
                friendList.Friend_list_id = sdr.GetInt32(0);
                if (!sdr.IsDBNull(1))
                    friendList.User_id = sdr.GetInt32(1);
                if (!sdr.IsDBNull(2))
                    friendList.Description = sdr.GetString(2);
            }
            return friendList;
        }

        //Them friend
        public static void Insert(FriendList friendList)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@user_id", friendList.User_id);
            dbh.AddParameter("@description", friendList.Description);
            string cmd = "FriendList_Insert";
            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }

        //Sua friend
        public static void Update(FriendList friendList)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@friend_list_id", friendList.Friend_list_id);
            dbh.AddParameter("@user_id", friendList.User_id);
            dbh.AddParameter("@description", friendList.Description);
            string cmd = "FriendList_Update";
            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }

        //Xoa friend
        public static void Delete(int ID)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@friend_list_id", ID);
            string cmd = "FriendList_Delete";
            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }
    }
}